package util

import (
	"crypto/sha256"
	"strconv"
	"time"
)

func DynamicVerify(dynamic, privateKey string) bool {
	if dynamic == dynamicGenerate(privateKey) {
		return true
	}
	return false
}

func DynamicGenerate(privateKey string) string {
	return dynamicGenerate(privateKey)
}

func dynamicGenerate(privateKey string) string {
	now := []byte(strconv.Itoa(int(time.Now().Unix() / 60)))
	_256Obj := sha256.New()
	_256Obj.Write(now)
	_256Obj.Write([]byte(privateKey))
	_256Obj.Write(now)

	sum := _256Obj.Sum(nil)
	var dynamic string
	for i := 0; i < 30; i += 5 {
		dynamic += strconv.Itoa(int(sum[i] % 10))
	}
	return dynamic
}
